package com.fingard.util;

import java.util.concurrent.atomic.AtomicInteger;

public class TransIDHelper {
	private AtomicInteger integerID = new AtomicInteger();

    private int minValue;
    private int maxValue;
    private String idFormat;

    public TransIDHelper(int p_minValue, int p_maxValue)
    {
        minValue = p_minValue;
        maxValue = p_maxValue;
        integerID = new AtomicInteger(minValue - 1);
        idFormat = "%0"+String.valueOf(maxValue).length()+"d";
    }

    /// <summary>
    /// 获取ID字符串
    /// </summary>
    /// <returns></returns>
    public String getNextIDStr()
    {
        int tmpIntID = integerID.incrementAndGet();
        if (tmpIntID == (maxValue + 1))
        {
        	integerID = new AtomicInteger(minValue - 1);
        	tmpIntID = integerID.incrementAndGet();
        }
        return String.format(idFormat, tmpIntID);
    }
    
    public int getNextID()
    {
        int tmpIntID = integerID.incrementAndGet();
        if (tmpIntID == (maxValue + 1))
        {
        	integerID = new AtomicInteger(minValue - 1);
        	tmpIntID = integerID.incrementAndGet();
        }
        return tmpIntID;
    }
}
